package cn.jh.common.utils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Yichuan
 * @version 1.0
 */
public class CommonsUtil {

    /**
     * 返回成功信息
     *
     * @param message 成功消息内容
     * @param obj 返回参数对象
     * @return 返回结果
     */
    public static Map<String, Object> returnSuccess(String message, Object obj) {
        Map<String, Object> map = new HashMap<>();
        map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
        map.put(CommonConstants.RESULT, obj);
        map.put(CommonConstants.RESP_MESSAGE, message);
        return map;
    }

    /**
     * 返回成功信息
     *
     * @param message 成功消息内容
     * @return 返回结果
     */
    public static Map<String, String> returnSuccess(String message) {
        Map<String, String> map = new HashMap<>();
        map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
        map.put(CommonConstants.RESULT, null);
        map.put(CommonConstants.RESP_MESSAGE, message);
        return map;
    }

    /**
     * 返回成功信息
     *
     * @return 返回结果
     */
    public static Map<String, String> returnOK() {
        return returnSuccess("成功");
    }

    /**
     * 返回成功信息
     *
     * @param obj 返回参数对象
     * @return 返回结果
     */
    public static Map<String, Object> returnOK(Object obj) {
        Map<String, Object> map = new HashMap<>();
        map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
        map.put(CommonConstants.RESULT, obj);
        map.put(CommonConstants.RESP_MESSAGE, "成功");
        return map;
    }

    /**
     * 返回失败信息
     *
     * @param message 失败消息内容
     * @return 返回结果
     */
    public static Map<String, String> returnError(String message){
        Map<String, String> map = new HashMap<>();
        map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
        map.put(CommonConstants.RESP_MESSAGE, message);
        return map;
    }

    /**
     * 其他异常信息
     *
     * @param code 消息code
     * @param message 消息内容
     * @return 返回结果
     */
    public static Map<String, String> returnError(String code, String message){
        Map<String, String> map = new HashMap<>();
        map.put(CommonConstants.RESP_CODE, code);
        map.put(CommonConstants.RESP_MESSAGE, message);
        return map;
    }


    /**
     * 银行卡代码
     * @return
     */
    public static Map getBankCode(){
        Map map = new HashMap();
        map.put ("BOC", "104");
        map.put ("ICBC", "102");
        map.put ("CCB", "105");
        map.put ("BCOM",  "301");
        map.put ("CMB", "308");
        map.put ("CITIC", "302");
        map.put ("CMBC", "305");
        map.put ("CIB", "309");
        map.put ("CGB", "306");
        map.put ("PAB", "307");
        map.put ("PSBC", "403");
        map.put ("CEB", "303");
        map.put ("HXB", "304");
        map.put ("NBB", "408");
        map.put ("BOB", "317");
        map.put ("SPDB", "310");
        map.put ("ABC", "103");
        map.put ("CZB", "316");
        return map;
    }

    /**
     * 畅捷接口返回异常编号
     * @return
     */
    public static String freeQuickError (String key){
        Map map = new HashMap();
        map.put("AC00000","参数有误");
        map.put("AC00001","验签失败");
        map.put("AC00002","系统异常");
        map.put("PB000000","通用系统异常异常码");
        map.put("PB000001","参数不能为空异常");
        map.put("PB000002","该通道未绑定银行卡");
        map.put("PB000003","订单不存在异常");
        map.put("PB000004","加解密失败异常");
        map.put("PB000005","请求底层通道异常");
        map.put("PB000006","配置信息为空");
        map.put("PB010001","重复请求");
        map.put("PB010002","结算异常");
        map.put("PB010003","通道异常");
        map.put("PB010004","订单记录已存在");
        map.put("PB010005","订单信息异常");
        map.put("PB010006","费率配置错误异常码");
        map.put("PB010007","通道验证码异常");
        map.put("PB010008","通道订单不存在");
        map.put("PB010009","通道商户信息异常");
        map.put("PB010010","通道金额异常");
        map.put("PB020004","服务商不存在");
        map.put("PB020005","服务商已存在");
        map.put("PB020006","结算方式异常");
        map.put("PB020007","商户角色异常");
        map.put("PB020008","账户信息不存在");
        map.put("PB020009","该商户产品已开通");
        map.put("PB020010","绑卡记录已存在");
        map.put("PB020011","费信息不存在");
        map.put("PB020012","费信息已存在");
        map.put("PB020013","商户不存在");
        map.put("PB020015","操作失败，用户余额不足");
        map.put("PB030001","账务系统内部系统异常码");
        map.put("PB030002","计费失败异常码");
        map.put("PB030003","费率配置错误异常码");
        map.put("PB030004","调用路由订单查询异常码");
        map.put("PB030005","未查询商户账单异常码");
        map.put("PB040001","商户开通产品异常");
        map.put("PB040002","商户更改产品异常");
        map.put("PB040003","商户更改结算信息异常");
        map.put("PB040004","注册商户异常");
        String value = (String)map.getOrDefault(key,"0");
        return value;
    }

    /**
     * 查询快捷商旅（代还商旅05）支持银行列表
     * @param key
     * @return
     */
    public static int verifyBankInfo(String key){
        int count = 0;
        Map map = new HashMap();
        map.put("104","中国银行");
        map.put("102","中国工商");
        map.put("105","建设银行");
        map.put("303","光大银行");
        map.put("313","北京银行");
        map.put("305","民生银行");
        map.put("306","广发银行");
        map.put("309","兴业银行");
        map.put("307","平安银行");
        map.put("103","农业银行");
        map.put("201","国家开发银行");
        map.put("202","中国进出口银行");
        map.put("203","中国农业发展银行");
        map.put("302","中信银行");
        map.put("304","华夏银行");
        map.put("308","招商银行");
        map.put("315","恒丰银行");
        map.put("316","浙商银行");
        map.put("501","汇丰银行");
        map.put("502","东亚银行");
        map.put("503","南洋商业银行");
        map.put("504","恒生银行");
        map.put("307","深圳发展银行");
        map.put("313","杭州银行");
        map.put("313","南京银行");
        map.put("402","北京农村商业银行");

//        map.put("102","工商银行");
//        map.put("301","交通银行");
//        map.put("103","农业银行");
//        map.put("501","汇丰银行");
        String value = (String) map.getOrDefault(key,"0");
        if(!value.equals("0")){
            count = 1;
        }
        return count;
    }

    /**
     * 查询畅捷新无卡支持银行列表
     * @param key
     * @return
     */
    public static int verifyNewCardFreevv(String key, String channelTag) {
        int count = 0;
        Map map = new HashMap();
        //CHANPAYNEWCARD_0101
        if (CommonConstants.CHANNEL_TAG_0101.equals(channelTag)) {
            map.put("104","中国银行");
            map.put("105","建设银行");
            map.put("302","中信银行");
            map.put("306","广发银行");
            map.put("305","民生银行");
            map.put("307","平安银行");
            map.put("304","华夏银行");
            map.put("313","北京银行");
            map.put("313","上海银行");
            map.put("303","光大银行");
            map.put("403","中国邮政储蓄银行");
            map.put("301","交通银行");
            map.put("308","招商银行");
            map.put("309","兴业银行");
            map.put("310","浦发银行");
        }

        //CHANPAYNEWCARD_0102
        if (CommonConstants.CHANNEL_TAG_0102.equals(channelTag)) {
            map.put("104","中国银行");
            map.put("105","建设银行");
            map.put("302","中信银行");
            map.put("306","广发银行");
            map.put("305","民生银行");
            map.put("307","平安银行");
            map.put("304","华夏银行");
            map.put("313","北京银行");
            map.put("313","上海银行");
            map.put("303","光大银行");
            map.put("403","中国邮政储蓄银行");
            map.put("301","交通银行");
            map.put("308","招商银行");
            map.put("309","兴业银行");
            map.put("310","浦发银行");
        }

        //新无卡地方性银行
        map.put("311", "恒丰银行");
        map.put("317", "渤海银行");
        map.put("402", "厦门银行");
        map.put("406", "吉林银行");
        map.put("408", "宁波银行");
        map.put("409", "齐鲁银行");
        map.put("412", "温州银行");
        map.put("413", "广州银行");
        map.put("414", "汉口银行");
        map.put("420", "大连银行");
        map.put("421", "苏州银行");
        map.put("422", "河北银行");
        map.put("423", "杭州银行");
        map.put("424", "南京银行");
        map.put("425", "东莞银行");
        map.put("429", "成都银行");
        map.put("434", "天津银行");
        map.put("436", "宁夏银行");
        map.put("442", "哈尔滨银行");
        map.put("440", "徽商银行");
        map.put("441", "重庆银行");
        map.put("444", "西安银行");
        map.put("450", "青岛银行");
        map.put("451", "吉林市商业银行");
        map.put("464", "泉州银行");
        map.put("474", "内蒙古银行");
        map.put("478", "南宁银行");
        map.put("480", "连云港银行");
        map.put("489", "南粤银行");
        map.put("491", "桂林银行");
        map.put("494", "徐州银行");
        map.put("495", "柳州银行");
        map.put("506", "温州市商业银行");
        map.put("507", "武汉市商业银行");
        map.put("508", "江苏银行股份有限公司");
        map.put("521", "江苏银行");
        map.put("530", "稠州银行");
        map.put("587", "厦门国际");
        map.put("588", "海南银行");
        map.put("589", "邯郸银行");
        map.put("591", "三湘银行");
        String value = (String) map.getOrDefault(key,"0");
        if(!value.equals("0")){
            count = 1;
        }
        return count;
    }

    /**
     * 在线收款费率
     * @param amounts 计算费率金额
     * @param rate 提现费率
     * @param extraFee 提现手续费
     * @return
     */
    public static BigDecimal avgBigDecimal(BigDecimal amounts , Double rate, Double extraFee){
        BigDecimal rte = new BigDecimal(rate);
        BigDecimal extraFees = new BigDecimal(extraFee);
        BigDecimal sum = amounts.multiply(rte);
        BigDecimal fee = sum.add(extraFees);
        BigDecimal mal =  amounts.add(fee);
        BigDecimal avg = mal.setScale(2,BigDecimal.ROUND_UP);
        return avg;
    }



    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 根据角色代码获取角色名称
     * @param roleCode
     * @return
     */
    public static String getRoleName(String roleCode) {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("r01", "系统管理");
        resMap.put("r02", "贴牌商");
        resMap.put("r03", "代理商");
        resMap.put("r04", "审核员");
        resMap.put("r05", "普通用户");
        String roleName = resMap.getOrDefault(roleCode, "未知").toString();
        return roleName;
    }

    /**
     * 畅捷交易、代还/提现状态查询
     * @param key 畅捷通道返回交易状态值
     * @return 0:待交易，1：成功，2：失败，3：异常，4：交易中；
     */
    public static String getResultStatus(String key) {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("UNPAY", "0");//待确认
        resMap.put("SUCCESS", "1");//支付成功
        resMap.put("FAILURE", "2");//支付失败
        resMap.put("FAIL", "2");//支付失败
        resMap.put("UNKNOW", "3");//未知
        resMap.put("PROCESS", "4");//支付中
        String status = resMap.getOrDefault(key, "4").toString();
        return status;
    }

    /**
     * 畅捷通道
     * @param id
     * @return
     */
//    public static String getChannelTag(int id){
//        String value = "";
//        if(id == 1){
//            //代还商旅05
//            value = CommonConstants.CHANNEL_TAG_04;
//        }else if(id == 2){
//            //新无卡代还101001
//            value = CommonConstants.CHANNEL_TAG_0101;
//        }else if(id == 3){
//            //新无卡代还101002
//            value = CommonConstants.CHANNEL_TAG_0102;
//        }else if(id == 4){
//            //快捷商旅05
//            value = CommonConstants.CHANNEL_TAG_05;
//        } else if (id == 5) {
//            value = CommonConstants.CHANNEL_TAG_05;
//        } else if (id == 6) {
//            value = CommonConstants.CHANNEL_TAG_05;
//        }
//        return value;
//    }

    /**
     * 畅捷验证信息是否重复
     * @param key
     * @return
     */
    public static boolean verifyError(String key){
        boolean count = false;
        if(key.equalsIgnoreCase("PB010004")){
            count = true;
        }else if(key.equalsIgnoreCase("PB020005")){
            count = true;
        }else if(key.equalsIgnoreCase("PB020010")){
            count = true;
        }else if(key.equalsIgnoreCase("PB020012")){
            count = true;
        }else if(key.equalsIgnoreCase("PB010001")){
            count = true;
        }else if(key.equalsIgnoreCase("PB020009")){
            count = true;
        }
        return count;
    }

    /**
     * 交易状态
     * @param status
     * @return
     */
    public static String queryStatus(String status){
        if(status.equalsIgnoreCase(CommonConstants.UNPAY)){//待确认
            status = "4";
        }else if(status.equalsIgnoreCase(CommonConstants.PROCESS)){//支付中
            status = "0";
        }else if(status.equalsIgnoreCase(CommonConstants.BASE_SUCCESS)){//支付成功
            status = "1";
        }else if(status.equalsIgnoreCase(CommonConstants.FAILURE)){//支付失败
            status = "2";
        }else if(status.equalsIgnoreCase(CommonConstants.UNKNOW)){//未知
            status = "3";
        }else if(status.equalsIgnoreCase(CommonConstants.REMITING)){
            status = "5";
        }
        return status;
    }


    public static String queryProductName(){
        List<String> consumeTypeName = new ArrayList<>();
        consumeTypeName.add("娱乐");
        consumeTypeName.add("购物");
        consumeTypeName.add("其他");
        consumeTypeName.add("通信");
        consumeTypeName.add("交通");
        consumeTypeName.add("住宿");
        consumeTypeName.add("餐饮");
        consumeTypeName.add("电子产品");
        consumeTypeName.add("化妆品");
        consumeTypeName.add("医美");
        int index = (int) (Math.random()* consumeTypeName.size());
        String productName = consumeTypeName.get(index);
        return productName;
    }

    public static String jhKey(String keyPlan) {
        String jhKey;
        switch (keyPlan) {
            case CommonConstants.NOTICE :
                jhKey = CommonConstants.NOTICE_JH_KEY;
                break;
            case CommonConstants.NAME_AUTH :
                jhKey = CommonConstants.NAMEAUTH_JH_KEY;
                break;
            case CommonConstants.BANK_AUTH :
                jhKey = CommonConstants.BANKAUTH_JH_KEY;
                break;
            case CommonConstants.BANK_TAG :
                jhKey = CommonConstants.BANK_TAG_JH_KEY;
                break;
            case CommonConstants.BANK_ASCRIPTION :
                jhKey = CommonConstants.ASCRIPTION_JH_KEY;
                break;
            default:
                jhKey = null;
                break;
        }
        return jhKey;
    }

    public static String getNumberOrderCode(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date now = new Date();
        StringBuffer sb = new StringBuffer(sdf.format(now));
        Random random = new Random();
        for(int i = 0;i < 18;i++){
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }
}
